<?php


class Project extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Project_model');
        $this->load->model('User_model');
    }

    public function detail($proid)
    {
        $re = $this->Project_model->getlist(array('project.proid'=> $proid), null, null);
        $uid = $re[0]['uid'];
        $user = $this->User_model->search(null, $uid);
        $this->load->view('project', array(
            'project'=>$re[0],
            'user'=>$user[0]
        ));
    }

    public function prolist($typeid = 1) {
        $protype = $this->Project_model->protype();
        $where = null;
        if ($typeid != 1) {
            $where = array(
                'project.typeid'=>$typeid
            );
        }
        $re = $this->Project_model->getlist($where, null, null);
        $this->load->view('prolist', array(
            'projects'=> $re,
            'protype'=>$protype
        ));
    }

    public function publish() {
        $protype = $this->Project_model->protype();
        $this->load->view('publish', array(
            'edit'=>false,
            'protype'=>$protype
        ));
    }


    public function create() {
        $uid = 1;
        $content = $_POST['content'];
        $title = $_POST['title'];
        $desc = $_POST['desc'];
        $cover = $_POST['cover'];
        $typeid = $_POST['typeid'];

        $data = array(
            'uid'=>$uid,
            'title'=>$title,
            'content'=>$content,
            'prodesc'=>$desc,
            'cover'=>$cover,
            'typeid'=>$typeid
        );
        $rs = $this->Project_model->create($data);

        $msg = "成功";
        $succ = 1;
        if ($rs) {
//            $result = $this->mycreatedir('images_tmp'.$rs);
            $msg = "成功";

//            $file = getcwd().'/images_tmp/'.$cover;
//            $newfile = getcwd().'/images/'.$cover;
//            copy($file,$newfile); //拷贝到新目录
//            unlink($file);
        } else {
            $msg = "失败";
            $succ = 0;
        }

        $this->echoJson('', $msg, $succ);
    }

    public function editpro() {
        $proid = $_POST['proid'];
        $content = $_POST['content'];
        $title = $_POST['title'];
        $desc = $_POST['desc'];
        $cover = $_POST['cover'];
        $typeid = $_POST['typeid'];
        $data = array(
            'title'=>$title,
            'content'=>$content,
            'prodesc'=>$desc,
            'cover'=>$cover,
            'typeid'=>$typeid
        );
        $msg = "成功";
        $succ = 1;
        $rs = $this->Project_model->update($proid, $data);
        if ($rs) {

        } else {
            $msg = "失败";
            $succ = 0;
        }
        $this->echoJson('', $msg, $succ);
    }

    public function edit($proid) {
        $protype = $this->Project_model->protype();
        $re = $this->Project_model->getlist(array('proid'=> $proid), null, null);
        $this->load->view('publish', array(
            'project'=>$re[0],
            'edit'=>true,
            'protype'=>$protype
        ));
    }
}